<?php
Database($_SESSION['cid']);
header("content-type:text/html;charset=utf-8");
class EventAction extends Action{

	public function index(){
		$rs=pageing('event',7,'',$_GET['seach'],'event_id desc','1','event_name','',"DB_MEETING");
		foreach($rs['data'] as $val){
			$val['event_startTime'] =date("Y-m-d H:i",$val['event_startTime']);
			$val['event_endTime']   =date("Y-m-d H:i",$val['event_endTime']);
			$val['project_name']    =M('project','','DB_MEETING')->where('project_id='.$val['project_id'])->getField('project_name');
			$val['event_launch_name']=D('Audience')->get_name_by_id($val['event_launch_id']);
			$arr[]=$val;
		}
		$rs['data']=$arr;
		$this->assign("rs",$rs);
		$this->display();
	}
//搜索
	public function search(){
		if($_POST){
			$event['_string'] = '(event_name like "%'.$_POST['event_name'].'%") OR (project_name like "%'.$_POST['project_name'].'%")';
			if($_POST['event_startTime']!=""){
				$time = strtotime($_POST['event_startTime']);		
				$event['event_startTime'] = array("egt",$time);
			};					
			$search_event = M('event','','DB_MEETING')->where($event)->order('event_id desc')->select();			
			$count = count($search_event);
			$pagesize = 10;
			$totalPage =ceil($count/$pagesize);		
			$page = $_POST['page'];
			$event_data = array_slice($search_event,($page-1)*$pagesize,$pagesize);			
			$rs['data'] = $event_data;
			if($page>1){
				$rs['show']="<li style='cursor:pointer'><a class='first' name='1' title='First Page'>&laquo; 首页</a></li><li style='cursor:pointer'><a class='prev' title='Previous Page'>&laquo; 上一页</a></li>";	
			}
			$offset = $pagesize*($page-1);
			$init = 1;
			$max = $totalPage;
			$pagelen=9;
			$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
			$pageoffset = ($pagelen-1)/2;
		
			if($totalPage>$pagelen){
				if($page<=$pageoffset){
					$init=1;
					$max = $pagelen;
				}else{
					if($page+$pageoffset>=$totalPage+1){
						$init = $totalPage-$pagelen+1;
					}else{
						$init = $page-$pageoffset;
						$max = $page+$pageoffset;
					}
				}
			}
		//生成html
			for($i=$init;$i<=$max;$i++){
				$current=$i==$page?'current':'';
				$rs['show'].="<li style='cursor:pointer'><a class='number {$current}' title='{$i}'>{$i}</a></li>";
			}
			if($page<$totalPage){
				$rs['show'].="<li style='cursor:pointer'><a class='next' title='Next Page'>下一页 &raquo;</a></li><li style='cursor:pointer'><a class='last' name='{$totalPage}' title='Last Page'>尾页 &raquo;</a></li>";
			}
			foreach($rs['data'] as $key=>$val){
			$val['event_startTime']=date("Y-m-d H:i",$val['event_startTime']);
			$val['event_endTime']=date("Y-m-d H:i",$val['event_endTime']);
			$arr[$key]=$val;
			};			
			$rs['data'] = $arr;
			$this->assign("rs",$rs);			
			$this->display();
		}
	}
	//删
	public function delAct(){
        
		$result = D('Company/Event')->event_delete($_GET['event_id']);
		if ($result){
			echo '{"state":1}';
		}else{
			echo '{"state":0}';
		}
    }
	//改
	public function to_update(){
		if($_POST){
			$event = D('Company/Event')->find_event($_POST['event_id']);
			$event_log = D('Company/Event')->event_log_data($_POST['event_id']);
			foreach($event_log as $key=>$value){			
				switch($value['event_state']){
					case 6:
						$event_log[$key]['state'] = "已发起";
					break;
					case 1:
						$event_log[$key]['state'] = "已审核";
					break;
					case 2:
						$event_log[$key]['state'] = "已提交";
					break;
					case 3:
						$event_log[$key]['state'] = "已执行";
					break;
					case 4:
						$event_log[$key]['state'] = "已验收";
					break;
					case 5:
						$event_log[$key]['state'] = "已归档";
					break;
				};
			};
			
			$event_executionlog = D('Company/Event')->event_executionlog_data($_POST['event_id']);
			$this->assign('event',$event);
			$this->assign('event_log',$event_log);
			
			$this->assign('event_executionlog',$event_executionlog);
			$this->display();
		}
	}
	public function uptAct(){
		$rs = M('event','','DB_MEETING')->where('event_id='.$_POST['event_id'])->save($_POST);
		if($rs){
			$this->success("Update Success!");
		} else {
			$this->error("update Fail for database");
		}
	}
	
	public function imgUpload(){
		
		$event_image_path = 'Public/Uploads'.'/';
		if(!is_dir($event_image_path))mkdir($event_image_path,0777,true);
		$event_image_path.= 'event'.'/';
		if(!is_dir($event_image_path))mkdir($event_image_path,0777,true);
		$event_image_path.= 100 .'/';
		if(!is_dir($event_image_path))mkdir($event_image_path,0777,true);

		import('ORG.Net.UploadFile');
		$config['savePath'] = $event_image_path;
		$config['allowExts']= array('jpg', 'gif', 'png', 'jpeg');
		$config['maxSize']= 3145728;
		$config['thumb'] = true;
		$config['thumbPrefix'] = 'm_,s_';
		$config['thumbMaxWidth'] = '480,240';
		$config['thumbMaxHeight'] = '320,160';
		$config['thumbRemoveOrigin']=true;
		$upload = new UploadFile($config);
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['event_image']	=$event_image_path.'m_'.$uploadList[0]['savename'];
			$_POST['event_image_s']	=$event_image_path.'s_'.$uploadList[0]['savename'];
		}
	}
	public function event_display(){
		$rs=M('event','','DB_MEETING')->where('event_id='.$_GET['event_id'])->save(array('event_display'=>$_GET['event_display']));
		if($rs)
			echo '{"state":0}';
		else
			echo '{"state":-1}';
	}
}	
?>
